package org.bleep.geom;

public class Rectangle
{
	private float x, y, w, h;
	
	public Rectangle()
	{
		x = 0.0f;
		y = 0.0f;
		w = 1.0f;
		h = 1.0f;
	}
	
	public Rectangle(float x, float y, float w, float h)
	{
		this.x = x;
		this.y = y;
		this.w = w;
		this.h = h;
	}
	
	public float getX()
	{
		return x;
	}
	
	public float getY()
	{
		return y;
	}
	
	public float getWidth()
	{
		return w;
	}
	
	public float getHeight()
	{
		return h;
	}
	
	public void translate(float x, float y)
	{
		this.x += x;
		this.y += y;
	}
	
	public void snap()
	{
		this.x = (int)(x + 0.5f);
		this.y = (int)(y + 0.5f);
	}
	
	public void setCenter(float cX, float cY)
	{
		this.x = cX - (w / 2.0f);
		this.y = cY - (h / 2.0f);
	}
	
	public float centerX()
	{
		return x + (w / 2.0f);
	}
	
	public float centerY()
	{
		return y + (h / 2.0f);
	}
	
	public void scale(float xFactor, float yFactor)
	{
		float tempX = centerX();
		float tempY = centerY();
		w = w * xFactor;
		h = h * yFactor;
		setCenter(tempX, tempY);
	}
	
	public void grow(float x, float y)
	{
		w += x;
		h += h;
	}
	
	public boolean intersects(Rectangle other)
	{
		if (x > other.getX() + other.getWidth()) return false;
		if (y > other.getY() + other.getHeight()) return false;
		if (x + w < other.getX()) return false;
		if (y + h < other.getY()) return false;
		return true;
	}
	
	public Rectangle copy()
	{
		return new Rectangle(x, y, w, h);
	}
	
	public String toString()
	{
		return "" + x + ", " + y + " : " + w + " X " + h;
	}
}